package com.ly.elastic.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ly.elastic.pojo.User;
import com.ly.elastic.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author LY
 * @create 2024/1/17 11:55
 * @Description 用户控制层
 */
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    /**
     * 构造数据向elasticsearch添加数据
     */
    @PostMapping("/saveUser")
    public String saveUser(){
        log.info("构造数据向elasticsearch添加数据|saveUser");
        User user1 = new User(UUID.randomUUID().toString(), "李小明", 1, "T0-1", new Date(), "13566668888", "184172133@qq.com", "北京");
        User user2 = new User(UUID.randomUUID().toString(), "张大明", 2, "T0-2", new Date(), "13566660001", "huahua@qq.com", "南京");
        User user3 = new User(UUID.randomUUID().toString(), "李书鹏", 2, "T1-1", new Date(), "13566660002", "xiaobai@qq.com", "榆树");
        User user4 = new User(UUID.randomUUID().toString(), "韩小雪", 2, "T2-1", new Date(), "13566660002", "xiaobai@qq.com", "榆树");
        User user5 = new User(UUID.randomUUID().toString(), "董叔飞", 2, "T4-1", new Date(), "13566660002", "xiaobai@qq.com", "河北");
        User user6 = new User(UUID.randomUUID().toString(), "候明相", 2, "T5-1", new Date(), "13566660002", "xiaobai@qq.com", "下花园");
        User user7 = new User(UUID.randomUUID().toString(), "田明明", 2, "T3-1", new Date(), "13566660002", "xiaobai@qq.com", "东平");
        User user8 = new User(UUID.randomUUID().toString(), "王大伟", 2, "T4-1", new Date(), "13566660002", "xiaobai@qq.com", "西湖");
        User user9 = new User(UUID.randomUUID().toString(), "李雪明", 2, "T1-1", new Date(), "13566660002", "xiaobai@qq.com", "南昌");
        User user10 = new User(UUID.randomUUID().toString(), "朱小飞", 2, "T2-1", new Date(), "13566660002", "xiaobai@qq.com", "吉林");
        User user11 = new User(UUID.randomUUID().toString(), "牛大明", 2, "T1-1", new Date(), "13566660002", "xiaobai@qq.com", "长春");
        User user12 = new User(UUID.randomUUID().toString(), "关雪儿", 2, "T2-1", new Date(), "13566660002", "xiaobai@qq.com", "深圳");
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        list.add(user6);
        list.add(user7);
        list.add(user8);
        list.add(user9);
        list.add(user10);
        list.add(user11);
        list.add(user12);
        list.stream().forEach(u -> userService.save(u));
        return "数据添加成功";
    }

    /**
     * 查询User所有数据
     */
    @GetMapping("/queryAll")
    public JSONObject queryAll(){
        log.info("获取所有数据|queryAll");
        Iterable<User> users = userService.queryAll();
        for (User u:users){
            System.out.println(u);
        }
        return (JSONObject) JSONObject.toJSON(users);
    }

    /**
     * 根据id查询
     * @param id
     */
    @GetMapping("/queryUserById")
    public JSON queryUserById(@Param("id") String id){
        log.info("根据id查询数据|queryUserById|{}",id);
        User user = userService.queryUserById(id);
        return (JSON) JSONObject.toJSON(user);
    }

//    @GetMapping("/findByName")
//    public JSON findByName(@Param("name") String name){
//        log.info("根据名称查询数据|findByName|{}",name);
//        Pageable pageable = new PageRequest(1, 2);
//        Page<User> user = userService.findByName(name, pageable);
//        return (JSON) JSONObject.toJSON(user);
//    }

    /**
     * 删除
     * @param id
     */
    @PostMapping("/delete")
    public String deleteById(@Param("id") String id){
        log.info("删除elasticsearch中的某条数据|deleteById|{}",id);
        userService.deleteById(id);
        return "删除成功";
    }

    /**
     * 查找在elasticsearch中是否存在该名称对应的索引
     * @param name
     */
    @PostMapping("/isIndexName")
    public boolean isIndexName(String name){
        log.info("索引查询|isIndexName|{}", name);
        return userService.isIndexIsExists(name);
    }

    /**
     * 关键字搜索
     * @param keyword 关键字
     * @param pageIndex 分页
     * @param pageSize 条数
     */
    @PostMapping("/search")
    public List<Map<String, Object>> parse(@RequestParam("keyword") String keyword,
                                           @RequestParam("pageIndex") Integer pageIndex,
                                           @RequestParam("pageSize") Integer pageSize) {
        return userService.search(keyword,pageIndex,pageSize);
    }
}
